import IPCPosition
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
import numpy as np
import zczg

from IPC.Algorithm import Algorithm
from IPC.ttypes import AlgoType

class AlgorithmHandler:
    def __init__(self):
        self.log = {}

    def HandleImage(self, img):
        print("Receive New Image")
        imga: np.ndarray = np.frombuffer(img.data, np.uint8)
        imga = imga.reshape(img.height, img.width)
        return zczg.HandleImage(imga)

    def GetSettingList(self):
        print("Call GetSettingList")
       

    def SetSettingItem(self, setid, item):
        print("Call SetSettingItem")


if __name__ == "__main__":
    handler = AlgorithmHandler()
    processor = Algorithm.Processor(handler)
    transport = TSocket.TServerSocket(host="0.0.0.0", port=14005)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

    print("Starting the server...")
    server.serve()
    print("done.")
